//
//  BTRatingView.m
//  BaxTer
//
//  Created by wsj2015 on 16/10/13.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTRatingView.h"

@implementation BTRatingView

- (IBAction)confirmAction:(id)sender {
    
    if (self.confirmBlock) {
        self.confirmBlock(self.helpNess.value * 2,self.content.value * 2);
    }

}

- (void)setAbleToRate:(BOOL)ableToRate{
        _ableToRate = ableToRate;
    
        self.content.enabled = ableToRate;
        self.helpNess.enabled = ableToRate;
    if (ableToRate == NO) {
        self.titleL.text = @"您已经评过分了!您的评分是:";
        self.confirmBtn.enabled = YES;
    }
    
}

- (void)setHelp_score:(CGFloat)help_score{
    _help_score = help_score;
    self.helpNess.value =help_score /2;
    self.helpNess_score.text = [NSString stringWithFormat:@"%.f分",help_score ];
    
    
}
- (void)setContet_score:(CGFloat)contet_score{
    _contet_score = contet_score;
    self.content.value = contet_score /2;
    self.content_score.text = [NSString stringWithFormat:@"%.f分",contet_score ];
  
}


+ (instancetype)ratingView{
    BTRatingView *ragingView =[[NSBundle mainBundle] loadNibNamed:@"BTRatingView" owner:nil options:0].lastObject;
    ragingView.confirmBtn.enabled = NO;
    [ragingView.helpNess addObserver:ragingView
                          forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:nil];
    [ragingView.content addObserver:ragingView
                          forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:nil];
    
    return ragingView;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  
    if ([object isEqual:self.helpNess]) {
        if ([keyPath isEqualToString:@"value"]) {
            NSNumber *v =  change[@"new"];
            if (v.floatValue <=0) {
                v = @(0);
            }
            self.helpNess_score.text = [NSString stringWithFormat:@"%.f分",v.floatValue * 2];
            
            
        }
    }
    if ([object isEqual:self.content]) {
        if ([keyPath isEqualToString:@"value"]) {
            NSNumber *v =  change[@"new"];
            if (v.floatValue <=0) {
                v = @(0);
            }
            self.content_score.text = [NSString stringWithFormat:@"%.f分",v.floatValue * 2];
          
            
        }
    }
    if (self.helpNess.value > 0 && self.content.value > 0) {
        self.confirmBtn.enabled = YES;
    }else{
        self.confirmBtn.enabled = NO;
    }
   
   
}
- (void)dealloc{
    [self.helpNess removeObserver:self forKeyPath:@"value"];
     [self.content removeObserver:self forKeyPath:@"value"];
}
@end
